package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.utils.DateConvertUtils;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.manage.service.TransportVisitsLogManageService;
import com.hunttown.mes.manage.service.AnalysisManageManageService;
import com.hunttown.mes.rpc.domain.TransportVisitsLogDTO;
import com.hunttown.mes.rpc.domain.query.TransportVisitsLogDTOQuery;
import com.hunttown.common.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * created by wangjunfu on 2022-09-16 18:56:06
 */
@Controller
@RequestMapping(value = "/basic/visitslog")
public class VisitsLogController {

    private final static Logger logger = LoggerFactory.getLogger(VisitsLogController.class);

    private final TransportVisitsLogManageService transportVisitsLogService;
    private final AnalysisManageManageService manageService;

    @Autowired
    public VisitsLogController(TransportVisitsLogManageService transportVisitsLogService, AnalysisManageManageService manageService) {
        this.transportVisitsLogService = transportVisitsLogService;
        this.manageService = manageService;
    }

    /**
     * 首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @return model
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, TransportVisitsLogDTOQuery dtoQuery) {

        //日期（初始化30天）
        if (StringUtils.isBlank(dtoQuery.getDaterange())) {
            dtoQuery.setDaterange(String.format("%s ~ %s", DateConvertUtils.getPastDate(30), DateConvertUtils.getPastDate(0)));
        }

        dtoQuery.setOrderBy("order by id desc");
        Page<TransportVisitsLogDTO> dataPage = transportVisitsLogService.getForPage(dtoQuery);
        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        //日期
        if (StringUtils.isNotBlank(dtoQuery.getDaterange())) {
            String[] date = dtoQuery.getDaterange().split("~");
            if (date.length == 2) {
                model.addAttribute("startDate", date[0].trim());
                model.addAttribute("endDate", date[1].trim());
            }
        }

        return "basic/visitslog/index";
    }

}